﻿using MediatR;
using SlaughterItem.Common.Domains.Saleser;
using SlaughterItem.Common.Domains.UPCTable;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace GetSlaughterItem.API.Application.Commands.Week03.leos
{
    /// <summary>
    /// 销售汇总
    /// </summary>
    public class SalesSummarySuretenHandler : IRequestHandler<SalesSummarySuretenCommend, Result>
    {
        private readonly IBaseRepositoryGet<SalesSummary> SalesSummary;
        private readonly IBaseRepositoryGet<UPCData> UPCData;

        public SalesSummarySuretenHandler(IBaseRepositoryGet<SalesSummary> SalesSummary, IBaseRepositoryGet<UPCData> UPCData)
        {
            this.SalesSummary = SalesSummary;
            this.UPCData = UPCData;
        }

        public async Task<Result> Handle(SalesSummarySuretenCommend request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };

            try
            {
                var list = (from a in SalesSummary.GetAll()
                            join b in UPCData.GetAll() on a.MarkId equals b.GoodsMinuteId
                            select new
                            {
                                a.SalesSummaryId,
                                a.EmployeeName,
                                a.SalesTimes,
                                a.SalesNum,
                                a.SalesPeice,
                                a.ReturnNum,
                                a.ReturnPrice,
                                b.GoodsMinuteId,
                                b.UPCDataName,
                            }).ToList();


                if (list != null)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = list;
                    result.Msg = "查询成功";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
